package com.wardrobe.smartWardrobe.controller;


import com.wardrobe.smartWardrobe.common.R;
import com.wardrobe.smartWardrobe.entity.Media;
import com.wardrobe.smartWardrobe.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tky
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/media")
public class MediaController {
    @Autowired
    MediaService mediaService;


//    @PostMapping("/files")
//    public R<List<Media>> list() {
//        return R.success(mediaService.list());
//    }

    @RequestMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Media> upload(@RequestPart("filedata") MultipartFile multipartFile, @RequestParam(value = "objectName", required = false) String objectName, @RequestParam("type") String type) throws IOException {
        Media media = new Media();
        String filename = multipartFile.getOriginalFilename();
        File tempFile = File.createTempFile("minio", ".temp");
        multipartFile.transferTo(tempFile);
        String filePath = tempFile.getAbsolutePath();
        if (!("pic".equals(type) || "doc".equals(type)||"xls".equals(type))) {
            return R.error("文件类型错误");
        }
        media.setMname(filename);
        media.setMtype(type);
        media.setMsize(String.valueOf(multipartFile.getSize()));
        return R.success(mediaService.uploadFile(media, filePath, objectName));
    }

    @PostMapping("/checkfile")
    public R<Boolean> checkfile(@RequestParam("fileMd5") String fileMd5) throws Exception {
        return mediaService.checkFile(fileMd5);
    }


    @PostMapping("/checkchunk")
    public R<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                 @RequestParam("chunk") int chunk) throws Exception {
        return mediaService.checkChunk(fileMd5, chunk);
    }

    @PostMapping(value = "/uploadchunk")
    public R<Boolean> uploadchunk(@RequestParam("file") MultipartFile file,
                                  @RequestParam("fileMd5") String fileMd5,
                                  @RequestParam("chunk") int chunk) throws Exception {
        File tempFile = File.createTempFile("minio", "temp");
        file.transferTo(tempFile);
        String localFilePath = tempFile.getAbsolutePath();
        return mediaService.uploadChunk(fileMd5, chunk, localFilePath);
    }

    @PostMapping("/mergechunks")
    public R<Boolean> mergechunks(@RequestParam("fileMd5") String fileMd5,
                                  @RequestParam("fileName") String fileName,
                                  @RequestParam("chunkTotal") int chunkTotal) throws Exception {
        Media media = new Media();
        media.setMname(fileName);
        media.setMtype("video");
        return mediaService.mergechunks(fileMd5, chunkTotal, media);
    }


}

